package com.cloud.pole.shop.controller.order.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.shop.biz.order.ShopOrderBiz;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.model.entity.order.ShopOrderAdmin;
import com.cloud.pole.shop.service.order.OrderService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/order/order/admin")
@Slf4j
public class OrderAdminController extends BaseController<ShopOrderBiz, ShopOrder> {

	@Autowired
	private OrderService orderService;

	/**
	 * 分页查询订单
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param mobile
	 * @param templateName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiRestResponse<ShopOrderAdmin> list(@RequestHeader(defaultValue = "") String token,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(name = "searchType", defaultValue = "") String searchType,
			@RequestParam(name = "orderSn", defaultValue = "") String orderSn,
			@RequestParam(name = "deliName", defaultValue = "") String deliName,
			@RequestParam(name = "deliTel", defaultValue = "") String deliTel,
			@RequestParam(name = "startTime", defaultValue = "") String startTime,
			@RequestParam(name = "endTime", defaultValue = "") String endTime,
			@RequestParam(name = "marketCode", defaultValue = "") String marketCode) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("searchType", searchType);
			params.put("orderSn", orderSn);
			params.put("deliName", deliName);
			params.put("deliTel", deliTel);
			params.put("startTime", startTime);
			params.put("endTime", endTime);
			params.put("marketCode", marketCode);
			PageInfo<ShopOrderAdmin> pageInfo = orderService.selectPageShopOrderAdminVoList(token, params, pageNo,
					pageSize);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@RequestMapping(value = "/getOrderInfo/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ApiRestResponse<ShopOrderAdmin> getOrderInfo(@RequestHeader(defaultValue = "") String token,
			@PathVariable String id) {
		try {
			ShopOrderAdmin shopOrder = null;
			String msg = "成功";
			if (id != null) {
				shopOrder = orderService.selectOrderAdminById(token, id);
			} else {
				msg = "订单编号不能为空";
			}
			return ApiRestResponse.success(msg, shopOrder);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@PutMapping("/sendOrder/{id}")
	@ResponseBody
	public ApiRestResponse<ShopOrder> sendOrder(@PathVariable("id") String id, @RequestBody ShopOrder shopOrder,
			@RequestHeader(defaultValue = "") String token) {
		try {
			ShopOrder shopOrderById = orderService.selectOrderAdminById(token, id);
			ShopOrder order = new ShopOrder();
			Date date = new Date();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = df.format(date);
			Date datee = new Date();
			datee = df.parse(format);
			order.setSendTime(datee);
			order.setOrderStatus(4);
			order.setLogisticsStatus(shopOrder.getLogisticsStatus());
			order.setLogisticsType(shopOrder.getLogisticsType());
			order.setLogisticsCompany(shopOrder.getLogisticsCompany());
			order.setLogisticsInt(shopOrder.getLogisticsInt());
			order.setOrderSn(shopOrder.getOrderSn());
			String msg = "";
			int status=0;
			if (shopOrderById != null) {
				int addShopOrderShippingAddress = orderService.addShopOrderShippingAddress(order);
				if (addShopOrderShippingAddress > 0) {
					msg = "发货成功!";
					status=200;
				} else {
					msg = "系统出现异常,发货失败!";
					status=300;
				}
			}
			return ApiRestResponse.success(status, msg, null);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
}
